package club.baimahu.pencil.domain.entity.discount;

import club.baimahu.pencil.domain.entity.order.CommodityList;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 常见的门槛条件  1.既要总金额大于某值,并且总件数大于某值  2.总金额大于某值或总件数大于某值
 */
@Data
public class CommonCondition implements Condition{

    private double largerAmount;

    private BigDecimal largerMoney;

    private int type;

    public static CommonCondition orCondition(double largerAmount,BigDecimal largerMoney){
        CommonCondition condition = new CommonCondition();
        condition.setLargerAmount(largerAmount);
        condition.setLargerMoney(largerMoney);
        condition.setType(0);
        return condition;
    }

    public static CommonCondition andCondition(double largerAmount,BigDecimal largerMoney){
        CommonCondition condition = new CommonCondition();
        condition.setLargerAmount(largerAmount);
        condition.setLargerMoney(largerMoney);
        condition.setType(1);
        return condition;
    }

    private static final int orType = 0;
    private static final int andType = 1;


    @Override
    public boolean fit(CommodityList commodityList) {
        BigDecimal totalMoney = commodityList.totalMoney();
        double totalAmount = commodityList.totalAmount();

        if(this.type == andType){
            return totalAmount >= this.largerAmount && totalMoney.doubleValue() >= this.largerMoney.doubleValue();
        }
        if(this.type == orType){
            return totalAmount >= this.largerAmount || totalMoney.doubleValue() >= this.largerMoney.doubleValue();
        }
        return false;
    }
}
